﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using Telerik.Windows.Controls;
using Telerik.Windows.Controls.Charting;
using System.Data;
using System.Data.SqlClient;

namespace AbhiRuchiFoods
{
    /// <summary>
    /// Interaction logic for EmployeeAttendenceReport.xaml
    /// </summary>
    public partial class EmployeeAttendenceReport : UserControl
    {
        public EmployeeAttendenceReport()
        {
            InitializeComponent();
            bindGridInformation(0);
            bindGridPerSec(0);
            bindGridTempSec(0);
            loadPermanentChart(0);
            loadTemporaryChart(0);
        }

        private void bindGridTempSec(int dayDiff)
        {
            DataTable dt = new DataTable();
            dt = AbhiruchiDAL.GetTemporarySectionAttendenceInformation(dayDiff);
            gvTempSec.ItemsSource = dt;
        }

        private void bindGridPerSec(int dayDiff)
        {
            DataTable dt = new DataTable();
            dt = AbhiruchiDAL.GetPermanentSectionAttendenceInformation(dayDiff);
            gvPerSec.ItemsSource = dt;
        }

        private void bindGridInformation(int dayDiff)
        {
            DataTable dt = new DataTable();
            dt = AbhiruchiDAL.GetEmployeeAttendenceInformation(dayDiff);
            gvInformation.ItemsSource = dt;
        }

        private void loadPermanentChart(int dayDiff)
        {
            DataTable tbl = new DataTable();

            DataColumn col = new DataColumn("Section");
            col.DataType = typeof(string);
            tbl.Columns.Add(col);
            col = new DataColumn("NoOfEmployees");
            col.DataType = typeof(int);
            tbl.Columns.Add(col);

            DataTable dt = new DataTable();
            dt = AbhiruchiDAL.GetPermanentSectionAttendenceInformation(dayDiff);
            for (int i = 0; i < dt.Rows.Count; i++)
            {
                dt.Rows[i][1] = 20;
            }

            foreach(DataRow drSource in dt.Rows)
            {
                DataRow drDestination = tbl.NewRow();
                drDestination[0] = drSource[0];
                drDestination[1] = drSource[1];
                tbl.Rows.Add(drDestination);
            }


            RdChartPermanentEmpAtt.DefaultView.ChartLegend.Header = "Sections";
            RdChartPermanentEmpAtt.SeriesMappings.Clear();
            SeriesMapping seriesMapping = new SeriesMapping();
            seriesMapping.SeriesDefinition = new PieSeriesDefinition();

            ItemMapping itemMapping = new ItemMapping();
            itemMapping.DataPointMember = DataPointMember.YValue;
            itemMapping.FieldName = "NoOfEmployees";
            seriesMapping.ItemMappings.Add(itemMapping);

            itemMapping = new ItemMapping();
            itemMapping.DataPointMember = DataPointMember.LegendLabel;
            itemMapping.FieldName = "Section";
            seriesMapping.ItemMappings.Add(itemMapping);

            RdChartPermanentEmpAtt.SeriesMappings.Add(seriesMapping);

            RdChartPermanentEmpAtt.ItemsSource = tbl;

        }

        private void loadTemporaryChart(int dayDiff)
        {
            DataTable tbl = new DataTable();

            DataColumn col = new DataColumn("Section");
            col.DataType = typeof(string);
            tbl.Columns.Add(col);
            col = new DataColumn("NoOfEmployees");
            col.DataType = typeof(int);
            tbl.Columns.Add(col);

            DataTable dt = new DataTable();
            dt = AbhiruchiDAL.GetTemporarySectionAttendenceInformation(dayDiff);
            for (int i = 0; i < dt.Rows.Count; i++)
            {
                dt.Rows[i][1] = 30;
            }

            foreach (DataRow drSource in dt.Rows)
            {
                DataRow drDestination = tbl.NewRow();
                drDestination[0] = drSource[0];
                drDestination[1] = drSource[1];
                tbl.Rows.Add(drDestination);
            }


            RdChartTemporaryEmpAtt.DefaultView.ChartLegend.Header = "Sections";
            RdChartTemporaryEmpAtt.SeriesMappings.Clear();

            SeriesMapping seriesMapping = new SeriesMapping();
            seriesMapping.SeriesDefinition = new PieSeriesDefinition();

            ItemMapping itemMapping = new ItemMapping();
            itemMapping.DataPointMember = DataPointMember.YValue;
            itemMapping.FieldName = "NoOfEmployees";
            seriesMapping.ItemMappings.Add(itemMapping);

            itemMapping = new ItemMapping();
            itemMapping.DataPointMember = DataPointMember.LegendLabel;
            itemMapping.FieldName = "Section";
            seriesMapping.ItemMappings.Add(itemMapping);

            RdChartTemporaryEmpAtt.SeriesMappings.Add(seriesMapping);

            RdChartTemporaryEmpAtt.ItemsSource = tbl;

        }

        private void btnToday_Click(object sender, RoutedEventArgs e)
        {
            bindGridInformation(0);
            bindGridPerSec(0);
            bindGridTempSec(0);
            loadPermanentChart(0);
            loadTemporaryChart(0);
        }

        private void btnPreviousday_Click(object sender, RoutedEventArgs e)
        {
            bindGridInformation(1);
            bindGridPerSec(1);
            bindGridTempSec(1);
            loadPermanentChart(1);
            loadTemporaryChart(1);
        }

        private void btnWeek_Click(object sender, RoutedEventArgs e)
        {
            bindGridInformation(7);
            bindGridPerSec(7);
            bindGridTempSec(7);
            loadPermanentChart(7);
            loadTemporaryChart(7);
        }

        private void btnMonth_Click(object sender, RoutedEventArgs e)
        {
            bindGridInformation(30);
            bindGridPerSec(30);
            bindGridTempSec(30);
            loadPermanentChart(30);
            loadTemporaryChart(30);
        }

        private void btnAllTime_Click(object sender, RoutedEventArgs e)
        {
            bindGridInformation(-1);
            bindGridPerSec(-1);
            bindGridTempSec(-1);
            loadPermanentChart(-1);
            loadTemporaryChart(-1);
        }        
    }
}
